//
//  HistoryViewController.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class HistoryViewController: UITableViewController {
    
    // MARK: - Properties
    
    private var historyModel = HistoryVCModel()
    
    
    // MARK: - Actions
    
    /// dismiss current view controller
    @objc private func goBack (sender: Any) {
        self.navigationController?.navigationBar.isHidden = true
        self.tabBarController?.tabBar.isHidden = false
        self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewWillAppear(_ animated: Bool) {
        self.setUpNavBar()
        self.setUpTable()
    }
    
    // MARK: - UI
    
    /// simple navigation bar with title, and custom back buttom
    private func setUpNavBar() {
        self.view.backgroundColor = .white
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.navigationBar.defaultNavBar()
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, landscapeImagePhone: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        self.navigationItem.simpleNavBar(title: "История платежей", backButton: backButton)
    }
    
    private func setUpTable () {
        tableView.backgroundColor = .whiteBlueColor
        tableView.register(HistoryCell.self, forCellReuseIdentifier: "historyCell")
        tableView.tableFooterView = UIView()
    }
    
    private func setSectionHeader (for section: Int) -> UIView {
        let secHeader = UIView()
        secHeader.backgroundColor = self.view.backgroundColor
        
        /// section header's label
        let titleLbl = UILabel(frame: .zero)
        titleLbl.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        titleLbl.textAlignment = .left
        titleLbl.sizeToFit()
        titleLbl.textColor = .greyColor
        titleLbl.text = historyModel.historyGroup[section].timestamp
        secHeader.addSubview(titleLbl)
        
        titleLbl.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().inset(12)
            make.leading.equalToSuperview().offset(16)
            make.height.equalTo(24)
        }
        
        return secHeader
    }
}




extension HistoryViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return historyModel.historyGroup.count
    }

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return historyModel.historyGroup[section].items.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell") as! HistoryCell
    
        cell.configure(with: historyModel.historyGroup[indexPath.section].items[indexPath.row])
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return self.setSectionHeader(for: section)
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 73.0
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60.0
    }
    
    // MARK: - Row Actions
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = historyModel.historyGroup[indexPath.section].items[indexPath.row]
        let receiptVC = ReceiptViewController()
        receiptVC.historyItem = item
        self.navigationController?.pushViewController(receiptVC, animated: true)
    }
}



